﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.ComponentModel;

using Com.Yoeso.Entity;
using Com.Yoeso.ORM;
using Com.Yoeso.ORM.Common;
using Com.Yoeso.ORM.Framework;

namespace Com.Yoeso.WebUI
{
    public class CalendarMaxRowtime
    {
        private Int64 maxeventrowtime;
        public Int64 MaxEventRowtime
        {
            get { return maxeventrowtime; }
            set { maxeventrowtime = value; }
        }
        private Int64 maxclassrowtime;
        public Int64 MaxClassRowtime
        {
            get { return maxclassrowtime; }
            set { maxclassrowtime = value; }
        }
    }

    [WebService(Namespace = "http://www.yoeso.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class calendar : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        //[WebMethod]
        //public string RegisterUser(String authstring)
        //{

        //    return "succeeded";
        //    return "failed";
        //}

        [WebMethod]
        public CalendarMaxRowtime GetMaxRowtime(String authstring, String memberID)
        {
            authstring = Public.DecodeBase64(authstring);
            memberID = Public.DecodeBase64(memberID);

            if (authstring != "CAB2D30CFD8B55881F85046CDD57059A")
                return null;

            try
            {
                CalendarMaxRowtime rowTime = new CalendarMaxRowtime();
                rowTime.MaxEventRowtime = 0;
                rowTime.MaxClassRowtime = 0;
                SqlBuilder sb = new SqlBuilder(SqlProvider.ORMBroker, Com.Yoeso.ORM.Framework.StatementType.Select, typeof(CalendarEvent));
                sb.SetRowLimit(1);
                sb.AddConstraint(Operator.Equals, "MemberID", memberID);
                sb.AddOrderByField(false, "RowTime");

                SqlResult sr = Broker.Execute(sb.GetStatement(true));
                IList lists = null;
                if (sr.Rows.Count > 0)
                {
                    lists = ObjectFactory.GetCollection(typeof(CalendarEvent), sr);
                    CalendarEvent ev = lists[0] as CalendarEvent;
                    rowTime.MaxEventRowtime = ev.RowTime;
                }

                sb = new SqlBuilder(SqlProvider.ORMBroker, Com.Yoeso.ORM.Framework.StatementType.Select, typeof(CalendarClass));
                sb.SetRowLimit(1);
                sb.AddConstraint(Operator.Equals, "MemberID", memberID);
                sb.AddOrderByField(false, "RowTime");

                sr = Broker.Execute(sb.GetStatement(true));
                lists = null;
                if (sr.Rows.Count > 0)
                {
                    lists = ObjectFactory.GetCollection(typeof(CalendarClass), sr);
                    CalendarClass ev = lists[0] as CalendarClass;
                    rowTime.MaxClassRowtime = ev.RowTime;
                }

                return rowTime;
            }
            catch (Exception ex)
            {
                String ee = ex.Message;
                ee += "";
            }

            return null;

        }

        [WebMethod]
        public CalendarClass[] GetServerClassData(String authstring,
            String memberid, Int64 maxEventRowTime)
        {
            authstring = Public.DecodeBase64(authstring);
            memberid = Public.DecodeBase64(memberid);

            if (authstring != "CAB2D30CFD8B55881F85046CDD57059A")
                return null;

            SqlBuilder sb = new SqlBuilder(SqlProvider.ORMBroker, Com.Yoeso.ORM.Framework.StatementType.Select, typeof(CalendarClass));
            //sb.SetRowLimit(1);
            sb.AddConstraint(Operator.Equals, "MemberID", memberid);
            sb.AddConstraint(Operator.GreaterThan, "rowtime", maxEventRowTime);
            //sb.AddOrderByField(false, "RowTime");

            SqlResult sr = Broker.Execute(sb.GetStatement(true));
            IList lists = null;
            if (sr.Rows.Count > 0)
            {
                lists = ObjectFactory.GetCollection(typeof(CalendarClass), sr);
            }

            CalendarClass[] newArray = new CalendarClass[lists.Count];

            for (int i = 0; i < lists.Count; i++)
                newArray[i] = lists[i] as CalendarClass;


            return newArray;
        }

        [WebMethod]
        public CalendarEvent[] GetServerEventData(String authstring,
            String memberid, Int64 maxEventRowTime)
        {
            authstring = Public.DecodeBase64(authstring);
            memberid = Public.DecodeBase64(memberid);

            if (authstring != "CAB2D30CFD8B55881F85046CDD57059A")
                return null;

            SqlBuilder sb = new SqlBuilder(SqlProvider.ORMBroker, Com.Yoeso.ORM.Framework.StatementType.Select, typeof(CalendarEvent));
            //sb.SetRowLimit(1);
            sb.AddConstraint(Operator.Equals, "MemberID", memberid);
            sb.AddConstraint(Operator.GreaterThan, "rowtime", maxEventRowTime);
            //sb.AddOrderByField(false, "RowTime");

            SqlResult sr = Broker.Execute(sb.GetStatement(true));
            IList lists = null;
            if (sr.Rows.Count > 0)
            {
                lists = ObjectFactory.GetCollection(typeof(CalendarEvent), sr);
            }

            CalendarEvent[] newArray = new CalendarEvent[lists.Count];

            for (int i = 0; i < lists.Count; i++)
                newArray[i] = lists[i] as CalendarEvent;


            return newArray;
        }

        [WebMethod]
        public MemberShip UserLogin(String authstring, String email)
        {
            authstring = Public.DecodeBase64(authstring);
            email = Public.DecodeBase64(email);
            if (authstring != "CAB2D30CFD8B55881F85046CDD57059A")
                return null;

            MemberShip member = MemberShip.RetrieveByName(email);
            if (member == null)
                return null;

            return member;
        }

        [WebMethod]
        public String RegisterUser(String authstring,
            String id,
            String email,
            String password,
            String userName,
            bool sex,
            String telephone,
            String birthday,
            String address)
        {
            authstring = Public.DecodeBase64(authstring);
            id = Public.DecodeBase64(id);
            email = Public.DecodeBase64(email);
            password = Public.DecodeBase64(password);
            userName = Public.DecodeBase64(userName);
            telephone = Public.DecodeBase64(telephone);
            birthday = Public.DecodeBase64(birthday);
            address = Public.DecodeBase64(address);

            if (authstring != "CAB2D30CFD8B55881F85046CDD57059A")
                return "authfailed";

            if (MemberShip.RetrieveByName(email) != null)
                return "userexist";


            try
            {
                MemberShip member = new MemberShip();
                member.ID = id;
                member.EMail = email;
                member.Password = password;
                member.Username = userName;
                member.Sex = sex;
                member.Telephone = telephone;
                member.Birthday = DateTime.ParseExact(birthday, "yyyy-M-d hh:ss:mm", null);
                member.Address = address;
                MemberShip.Insert(member);
                return "succeeded";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            return "failed";
        }

        [WebMethod]
        public String UpdateClassData(String authstring,
            String id,
            String name,
            String desc,
            bool isprivate,
            bool isshow,
            String memberid,
            int red,
            int green,
            int blue,
            bool isdelete,
            Int64 createtime,
            Int64 rowtime)
        {
            authstring = Public.DecodeBase64(authstring);
            id = Public.DecodeBase64(id);
            name = Public.DecodeBase64(name);
            desc = Public.DecodeBase64(desc);
            memberid = Public.DecodeBase64(memberid);

            if (authstring != "CAB2D30CFD8B55881F85046CDD57059A")
                return "authfailed";



            CalendarClass data = CalendarClass.Retrieve(id);
            bool add = false;
            if (data == null)
            {
                data = new CalendarClass();
                add = true;
            }
            data.ID = id;
            data.Name = name;
            data.Desc = desc;
            data.IsPrivate = isprivate;
            data.IsShow = isshow;
            data.MemberID = memberid;
            data.Red = red;
            data.Green = green;
            data.Blue = blue;
            data.IsDelete = isdelete;
            data.CreateTime = createtime;
            data.RowTime = rowtime;
            if (add)
            {
                Broker.Insert(data);
            }
            else
            {
                Broker.Update(data);
            }

            return "succeeded";
        }


        [WebMethod]
        public String UpdateEventData(String authstring,
            String id,
            String memberid,
            String classid,
            String topic,
            String place,
            String desc,
            bool isfullday,
            bool islunar,
            String startdatetime,
            String enddatetime,
            int repeatype,
            int endtype,
            String endtypevalue,
            int remindtype,
            bool isdelete,
            Int64 createtime,
            Int64 rowtime)
        {
            authstring = Public.DecodeBase64(authstring);
            id = Public.DecodeBase64(id);
            memberid = Public.DecodeBase64(memberid);
            classid = Public.DecodeBase64(classid);
            topic = Public.DecodeBase64(topic);
            place = Public.DecodeBase64(place);
            desc = Public.DecodeBase64(desc);
            startdatetime = Public.DecodeBase64(startdatetime);
            enddatetime = Public.DecodeBase64(enddatetime);
            endtypevalue = Public.DecodeBase64(endtypevalue);

            if (authstring != "CAB2D30CFD8B55881F85046CDD57059A")
                return "authfailed";

            CalendarEvent data = CalendarEvent.Retrieve(id);
            bool add = false;
            if (data == null)
            {
                data = new CalendarEvent();
                add = true;
            }
            data.ID = id;
            data.MemberID = memberid;
            data.ClassID = classid;
            data.Topic = topic;
            data.Place = place;
            data.Desc = desc;
            data.IsFullDay = isfullday;
            data.IsLunar = islunar;
            data.StartDateTime = DateTime.ParseExact(startdatetime, "yyyy-M-d h:s:m", null);
            data.EndDateTime = DateTime.ParseExact(enddatetime, "yyyy-M-d h:s:m", null);
            data.RepeatType = repeatype;
            data.EndType = endtype;
            data.EndTypeValue = endtypevalue;
            data.RemindType = remindtype;
            data.IsDelete = isdelete;
            data.CreateTime = createtime;
            data.RowTime = rowtime;

            if (add)
            {
                Broker.Insert(data);
            }
            else
            {
                Broker.Update(data);
            }

            return "succeeded";
        }
    }
}
